package demo3;

/**
 * Created with IntelliJ IDEA.
 * Description: 多态讲解
 * User: 阿宾
 * Date: 2024-05-19
 * Time: 18:31
 */
//父类
class Base{
    public int a;
    public int b;

    //构造方法
    public Base(int a, int b) {
        this.a = a;
        this.b = b;
    }
    //父类方法
    public void fun1(){
        System.out.println("父类方法");
    }

    @Override
    public String toString() {
        return "Base{" +
                "a=" + a +
                ", b=" + b +
                '}';
    }
}
//子类
class Derived extends Base {
    //构造方法
    public Derived(int a, int b) {
        super(a, b);
    }
    public void func2(){
        System.out.println("子类方法");
    }
}
public class Test01 {
    public static void main(String[] args) {
        Base b1 = new Base(1,2);
        System.out.println(b1);
        b1.fun1();
        //向上转型，父类引用指向子类对象
        Base b2 = new Derived(5,9);//方式1，直接赋值
        b2.fun1();
        //b2.fun2(); //error，只能调用父类方法，不能调用子类方法
    }
}
